package com.chauncy.DMS.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.faces.application.Application;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import util.List2Json1.List2Json1;

import com.chauncy.DMS.bean.ManagerInfoBean;
import com.chauncy.DMS.dao.ManagerDaoImplement;

public class SelectAllAttForBuildNameServlet extends HttpServlet {

	private String buildings;
	private ManagerDaoImplement daoImplement = new ManagerDaoImplement();
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			List<ManagerInfoBean> list = (List)request.getSession().getAttribute("managerInfoList");
			buildings = list.get(0).getManager_buildings();
			Object[] param1 = {buildings};
			int limit = 0;
			int page = 0;
			//搜索名字 获取单个信息
			String sName = request.getParameter("sName");
			System.out.println("SelectAllAttForBuildNameServlet: ::name:"+ sName);
			try {
				//获取page  limit
				 limit = Integer.parseInt(request.getParameter("limit"));
				 page = Integer.parseInt(request.getParameter("page"));
			} catch (Exception e) {
				// TODO: handle exception
			}
			
			String sqls = "select `sdit`.`Stu_name` AS `name`,`sdit`.`College` AS `College`,`sdit`.`Study_major` AS `major`,`sdit`.`The_Class` AS `tClass`,`sdit`.`Dorm_number` AS `dNub`,`sait`.`Is_leave` AS `Is_leave`,`sait`.`Time_for_leave` AS `Time_for_leave`,`sait`.`Stay_out_late` AS `Stay_out_late`,`sait`.`Late_return_time` AS `Late_return_time`,`sait`.`No_return` AS `No_return`,date_format(`sait`.`Att_date`, '%Y-%m-%d') AS `Att_date`,`sait`.`remarks` AS `Remarks` from (`stu_att_info_table` `sait` join `stu_dorm_info_table` `sdit`) where ((`sait`.`Stu_number` = `sdit`.`Stu_number`) and (`sdit`.`Dormitory_number` = ?))";
			//查询所有考勤记录
			String sql =sqls+ " ORDER BY sait.Att_date DESC limit ?,?;";
			//记录条目数
			String sql0 = "select count(*) from ("+sqls+") as allAtt;";
			//根据姓名学号获取学生信息
			if(sName != null && !sName.equals("")){
				Object[] param = {buildings,sName,((page-1)*limit),limit};
				//System.out.println("StudentName:"+sName);
				sqls = sqls +"and Stu_name = ?  ORDER BY sait.Att_date DESC limit ?,?;";
				List<Map<String,Object>> allAttList = daoImplement.query(sqls, param);
				String sqlCount = "select count(*) from" +
						" ( select `sdit`.`Stu_name` AS `name`," +
						"`sdit`.`College` AS `College`," +
						"`sdit`.`Study_major` AS `major`," +
						"`sdit`.`The_Class` AS `tClass`," +
						"`sdit`.`Dorm_number` AS `dNub`," +
						"`sait`.`Is_leave` AS `Is_leave`," +
						"`sait`.`Time_for_leave` AS `Time_for_leave`," +
						"`sait`.`Stay_out_late` AS `Stay_out_late`," +
						"`sait`.`Late_return_time` AS `Late_return_time`," +
						"`sait`.`No_return` AS `No_return`," +
						"date_format(`sait`.`Att_date`, '%Y-%m-%d') AS `Att_date`," +
						"`sait`.`remarks` AS `Remarks`" +
						" from (`stu_att_info_table` `sait` join `stu_dorm_info_table` `sdit`)" +
						" where ((`sait`.`Stu_number` = `sdit`.`Stu_number`)" +
						" and (`sdit`.`Dormitory_number` = ?))" +
						"  AND `sdit`.`Stu_name` = '"+sName+"' ) as allAtt;";
				JSONObject allAttJson = List2Json1.mapToJSON(allAttList, param1, sqlCount);
		
		//		request.setAttribute("allAttJson",allAttJson);
				response.getWriter().print(allAttJson);
				
			}else{
				Object[] param = {buildings,((page-1)*limit),limit};
				List<Map<String,Object>> allAttList = daoImplement.query(sql, param);
				
				JSONObject allAttJson = List2Json1.mapToJSON(allAttList, param1, sql0);
				System.out.println("allAtt");
				request.getSession().setAttribute("selectAttInfoJson",allAttList);
				
				response.getWriter().print(allAttJson);
			}
	}

	//考勤查询功能
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String sNub = request.getParameter("sNub");//获取学号
		String dNub = request.getParameter("dNub");//获取楼栋号
		String attDate = request.getParameter("attDate");//获取考勤日期
		attDate = "%"+attDate+"%";
		String sql = "call oneAttInfo(?,?,?);";
		Object[] param = {sNub,dNub,attDate};
		if(sNub != null && dNub != null && attDate != null){//当获取值不为空时查询
			System.out.println("doPost buildings :"+buildings);//执行查询
			List<Map<String,Object>> allAttList = daoImplement.query(sql, param);
			JSONArray allAttJson = JSONArray.fromObject(allAttList);//将其数据格式化
			ServletContext context = this.getServletContext();
			context.setAttribute("allAttJson",allAttList);
			response.getWriter().print(allAttJson);
		}
	}

}
